function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/ordertracking/list',
        datatype: "json",
        colModel: [			
			//{ label: 'number', name: 'number', index: 'number', width: 50, key: true },
			{ label: '客户编码', name: 'customnumber', index: 'customnumber', width: 80 }, 			
			{ label: '客户简称', name: 'customsimplename', index: 'customsimplename', width: 80 }, 			
			{ label: '客户全称', name: 'customname', index: 'customname', width: 80 }, 			
			{ label: '联系人', name: 'contactperson', index: 'contactperson', width: 80 },
			{ label: '联系电话', name: 'contactpersontel', index: 'contactpersontel', width: 80 }, 			
			{ label: '运单编号', name: 'waybill', index: 'waybill', width: 80 }, 			
			{ label: '提货单位', name: 'delivercompany', index: 'delivercompany', width: 80 }, 			
			{ label: '送货地址', name: 'deliveraddress', index: 'deliveraddress', width: 80 }, 			
			{ label: '送货联系人', name: 'deliverperson', index: 'deliverperson', width: 80 } 			
			//,{ label: '操作', name: 'id',  width: 80,formatter: function(cellvalue, options, rowdata) {
			//        return '<a href="orderview.html?id=' + cellvalue + '">查看</a>';
			//	}
			//}
/*,
			{ label: '提货电话', name: 'delivertel', index: 'delivertel', width: 80 }, 			
			{ label: '提空地点', name: 'pickemptyaddress', index: 'pickemptyaddress', width: 80 }, 			
			{ label: '提空联系人', name: 'pickemptyperson', index: 'pickemptyperson', width: 80 }, 			
			{ label: '提空联系人电话', name: 'pickemptytel', index: 'pickemptytel', width: 80 }, 			
			{ label: '还空地点', name: 'backemptyaddress', index: 'backemptyaddress', width: 80 }, 			
			{ label: '空还联系人', name: 'backemptyperson', index: 'backemptyperson', width: 80 }, 			
			{ label: '还空联系人电话', name: 'backemptytel', index: 'backemptytel', width: 80 }, 			
			{ label: '提重地点', name: 'pickweightaddress', index: 'pickweightaddress', width: 80 }, 			
			{ label: '重提联系人', name: 'pickweightperson', index: 'pickweightperson', width: 80 }, 			
			{ label: '重提联系人电话', name: 'pickweighttel', index: 'pickweighttel', width: 80 }, 			
			{ label: '还重地点', name: 'backweightaddress', index: 'backweightaddress', width: 80 }, 			
			{ label: '还重联系人', name: 'backweightperson', index: 'backweightperson', width: 80 }, 			
			{ label: '还重联系人电话', name: 'backweighttel', index: 'backweighttel', width: 80 }, 			
			{ label: '船名', name: 'shipname', index: 'shipname', width: 80 }, 			
			{ label: '航次', name: 'voyage', index: 'voyage', width: 80 }, 			
			{ label: '提单号', name: 'ladingbil', index: 'ladingbil', width: 80 }, 			
			{ label: '货名', name: 'desinname', index: 'desinname', width: 80 }, 			
			{ label: '件数', name: 'packages', index: 'packages', width: 80 }, 			
			{ label: '量重', name: 'weight', index: 'weight', width: 80 }, 			
			{ label: '寸尺', name: 'measurement', index: 'measurement', width: 80 }, 			
			{ label: '是否危险品', name: 'isdanger', index: 'isdanger', width: 80 }, 			
			{ label: '是否冷箱', name: 'iscoldbox', index: 'iscoldbox', width: 80 }, 			
			{ label: '冷箱温度', name: 'coldboxtemp', index: 'coldboxtemp', width: 80 }, 			
			{ label: '是否集装箱散货', name: 'iscontainerbulk', index: 'iscontainerbulk', width: 80 }, 			
			{ label: '业务类型（1.内贸，2.外贸）', name: 'businesstype', index: 'businesstype', width: 80 }, 			
			{ label: '提（送）货', name: 'ispickdelivery', index: 'ispickdelivery', width: 80 }, 			
			{ label: '集装箱信息外键', name: 'containerinfo', index: 'containerinfo', width: 80 }	
			*/		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		id: null,
		act: 'list',
		step: 1,
		showList: true,
		title: null,
		ordertracking: {},
		customerinfo: {},
		containerList: []
	},
	created:function(){
		this.id = getQueryString('id');
		var act = getQueryString('act');
		var step = getQueryString('step');
		this.act = act ? act : 'list';
		this.step = step ? step : '1';
		this.getInfo(this.id);
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			this.act = 'edit';
			vm.title = "新增";
			vm.ordertracking = {
				customname: '',
				customsimplename: '',
				contactperson: '',
				contactpersontel: '',
				isdanger:'否', 
				iscoldbox:'否'
			};
			//vm.ordertracking.customname = 'sdfsddsf';
		},
		update: function (event) {
			var id = getSelectedRow();
			console.log('id='+id);
			if(id == null){
				return ;
			}
			this.act = 'edit';
			this.step = 1;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		dispatch: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			this.act = 'edit';
			this.step = 2;
            vm.getInfo(id)
			//location.href = baseURL + 'modules/sys/container.html?orderId=' + id;
		},
		fee: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			this.act = 'edit';
			this.step = 3;
            vm.getInfo(id)
			//location.href = baseURL + 'modules/sys/container.html?orderId=' + id;
		},
		check: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			this.act = 'edit';
			this.step = 3;
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			var ordertracking = vm.ordertracking;
			if(!ordertracking.customnumber) {
				alert("客户编码不能为空");
				return;
			}
			if(!ordertracking.customsimplename) {
				alert("客户简称不能为空");
				return;
			}
			if(!ordertracking.customname) {
				alert("客户全称不能为空");
				return;
			}
			if(!ordertracking.contactperson) {
				alert("联系人不能为空");
				return;
			}
			if(!ordertracking.contactpersontel) {
				alert("联系电话不能为空");
				return;
			}
			// if(!ordertracking.waybill) {
			// 	alert("运单编号不能为空");
			// 	return;
			// }
			if(!ordertracking.type) {
				alert("请选择业务类型");
				return;
			}
			if(ordertracking.type == "进港") {
				//alert("请选择业务类型");
				//return;
			} else if(ordertracking.type == "出港") {
				//alert("请选择业务类型");
				//return;
			} else if(ordertracking.type == "临时") {
				//alert("请选择业务类型");
				//return;
			}
			if(!ordertracking.expectedkilo) {
				alert("请输入预计公里数");
				return;
			}
			var url = vm.ordertracking.id == null ? "sys/ordertracking/save" : "sys/ordertracking/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.ordertracking),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var numbers = getSelectedRows();
			if(numbers == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/ordertracking/delete",
                    contentType: "application/json",
				    data: JSON.stringify(numbers),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			if(id){
				//open(baseURL + "sys/ordertracking/info/"+id);
				$.get(baseURL + "sys/ordertracking/info/"+id, function(r){
	                vm.ordertracking = r.ordertracking;
	                if(!vm.ordertracking.isdanger){
	                	vm.ordertracking.isdanger = '否';
	                }
	                if(!vm.ordertracking.iscoldbox){
	                	vm.ordertracking.iscoldbox = '否';
	                }
	                vm.containerList = r.containerList;
	                console.log('vm.containerList.length = ' + vm.containerList.length);
	            });
			}
		},
		reload: function (event) {
			vm.act = 'list';
			// vm.showList = true;
			// vm.step=1;
			var page = $("#jqGrid").jqGrid('getGridParam','page');

			$("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");

			console.log('vm===='+vm.ordertracking.toString());
		},
		c_add: function () {
			var orderId = this.ordertracking.id;
			var waybill = this.ordertracking.waybill;
			if(orderId == null){
				return ;
			}
			location.href = baseURL + 'modules/sys/container.html?act=edit&orderId=' + orderId+'&waybill='+waybill;
		},
		c_view: function (id) {
			var orderId = this.ordertracking.id;
			if(orderId == null){
				return ;
			}
			//open(baseURL + 'modules/sys/container.html?act=edit&orderId=' + orderId + '&id=' + id);
			location.href = baseURL + 'modules/sys/containerview.html?orderId=' + orderId + '&id=' + id;
		},
		c_modify: function (id) {
			console.log('baseURL22222='+baseURL);
			var orderId = this.ordertracking.id;
			if(orderId == null){
				return ;
			}
			//open(baseURL + 'modules/sys/container.html?act=edit&orderId=' + orderId + '&id=' + id);
			location.href = baseURL + 'modules/sys/container.html?act=edit&orderId=' + orderId + '&id=' + id;
		},
		c_del: function (id) {
			//alert("="+id + "/"+JSON.stringify(id));
			//return;
			confirm('确定要删除选中的集装箱信息？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/container/delete/" + id,
                    contentType: "application/json",
				    //data: JSON.stringify(id),
				    //data: [12],
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								//$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		loadCustomerInfo: function () {
			var customnumber = vm.ordertracking.customnumber;
			//console.log(baseURL + 'sys/custominfo/info/' + customnumber);
			if(customnumber) {
				ajax({
					url: baseURL + 'sys/custominfo/info2/' + customnumber
				}, function(resp){
					if(resp.custominfo) {
						console.log('custominfo === ' + JSON.stringify(resp.custominfo));
						vm.customerinfo = resp.custominfo;
						vm.ordertracking.customsimplename=vm.customerinfo.customsimplename;
						vm.ordertracking.customname=vm.customerinfo.customname;
						vm.ordertracking.contactperson=vm.customerinfo.contactperson;
						vm.ordertracking.contactpersontel=vm.customerinfo.contactpersontel;
						//vm.ordertracking.telephone=vm.customerinfo.telephone;
					} else {
						alert('客户不存在');
					}
				});
			}
		}
	}
});